<template>
  <div class="shoparea">
    <div class="area_box">
      <div class="area_item">
        <div class="area_name">
          *联系人：
        </div>
        <div class="area_input">
          <input type="text" v-model="shop.name" @blur="inputname" placeholder="请输入姓名">
        </div>
      </div>
      <div class="area_item">
        <div class="area_name">
          *联系电话：
        </div>
        <div class="area_input">
          <input type="text" v-model="shop.phone" @blur="inputphone" placeholder="请输入联系电话">
        </div>
      </div>
      <div class="area_item">
        <div class="area_name">
          *所售类目：
        </div>
        <div class="area_input">
          <input type="text" v-model="shop.shop_type" @blur="inputtype" placeholder="所售类目">
        </div>
      </div>
      <div class="area_item">
        <div class="area_name">
          *店铺名称：
        </div>
        <div class="area_input">
          <input type="text" v-model="shop.mch_name" @blur="inputmchname" placeholder="请输入店铺名称">
        </div>
      </div>
      <div class="area_item">
        <div class="area_name">
          *所在省市：
        </div>
        <div class="area_input">
          <el-cascader size="large" :options="options" v-model="shop.areaarr" @change="handleChange">
          </el-cascader>
        </div>
      </div>
      <div class="area_item">
        <div class="area_name">
          *详细地址：
        </div>
        <div class="area_input">
          <input type="text" v-model="shop.address" @blur="inputaddress" placeholder="请输入联系电话">
        </div>
      </div>
      <div class="area_btns">
        <div class="area_btn" @click="shopfn">
          提交申请
        </div>
      </div>

    </div>
  </div>
</template>

<script>
  import {
    regionData,
    CodeToText,
    TextToCode
  } from 'element-china-area-data'
  import {
    getshopapi,
    setshopapi
  } from "../../../api/http.js"
  export default {
    name: 'shoparea',
    data() {
      return {
        options: regionData,
        shop: {
          name: "",
          phone: "",
          mch_name: "",
          address: "",
          area: "",
          shop_type: "",
          areaarr:""
        }
      }
    },
    created() {
      this._getshop()
    },
    methods: {
      handleChange(value) {
        if (!Array.isArray(value)) return
        this.shop.area=CodeToText[value[0]] + '/' + CodeToText[value[1]] + '/' + CodeToText[value[2]]
      },
      async _getshop() {
        const res = await getshopapi()
        if (res.data.code == 200) {
          // console.log(res.data.data,res.data.data.area);

          if(res.data.data.area!=undefined){
          res.data.data.areaarr=TextToCode[res.data.data.area.split("/")[0]][res.data.data.area.split("/")[1]][res.data.data.area.split("/")[2]].code
          }
          this.shop = res.data.data
        } else {
          this.shop = []
        }
      },
      // 商家数据的提交
      async shopfn() {
        if(localStorage.getItem('user')==null) {
          this.$message("还没有登录，请先登录")
          await this.$router.push({path: "/login"})
          return false
        }
        var namereg = /^[\u4e00-\u9fa5]+$/
        var phonereg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])[0-9]{8}$/
        var addressreg = /^[^ ]/
        if (this.shop.name == "" || !namereg.test(this.shop.name)) {
          return this.$message("输入正确的姓名")
        }
        if (this.shop.phone == "" || !phonereg.test(this.shop.phone)) {
          return this.$message("输入正确的手机号码")
        }
        if (this.shop.mch_name == "" || !namereg.test(this.shop.mch_name)) {
          return this.$message("输入正确的店铺名称")
        }
        if (this.shop.area == "") {
          return this.$message("请选择正确的地址")
        }
        if (this.shop.address == "" || !addressreg.test(this.shop.address)) {
          return this.$message("输入正确的详细地址")
        }
        if (this.shop.shop_type == "" || !addressreg.test(this.shop.shop_type)) {
          return this.$message("请选择所售类目")
        }
        console.log(this.shop);
        const res =await setshopapi(this.shop)
        if(res.data.code==200){
          this.$message("提交成功")
          this._getshop()
        }
      },
      // 姓名
      inputname() {
        var namereg = /^[\u4e00-\u9fa5]+$/
        namereg.test(this.shop.name) ? this.$message("姓名输入正确") : this.$message("输入正确的姓名")
      },
      // 电话号码
      inputphone() {
        var phonereg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])[0-9]{8}$/
        phonereg.test(this.shop.phone) ? this.$message("手机号输入正确") : this.$message("请输入正确的手机号")
      },
      // 类目
      inputtype() {
        var addressreg = /^[^ ]/
        addressreg.test(this.shop.shop_type) ? this.$message("输入正确") : this.$message("输入所卖的类目")
      },
      // 商家的名字
      inputmchname() {
        var namereg = /^[\u4e00-\u9fa5]+$/
        namereg.test(this.shop.mch_name) ? this.$message("店铺输入正确的") : this.$message("输入正确的店铺名称")
      },
      // 详细地址
      inputaddress() {
        var addressreg = /^[^ ]/
        addressreg.test(this.shop.address) ? this.$message("详细地址输入正确的") : this.$message("请输入正确的详细地址")
      }
    }
  }
</script>

<style lang="less">
  .shoparea {
    width: 100%;
    padding: 0 100px;
    height: 360px;

    .area_box {
      width: 100%;
      height: 360px;
      background-color: rgba(255,255,255,0.2);
      padding: 70px;
      box-sizing: border-box;

      .area_item {
        width: 50%;
        margin-bottom: 20px;
        height: 40px;
        display: flex;
        float: left;
        justify-content: space-between;
        align-items: center;

        .area_name {
          flex: 0.5;
          color: #292929;
          font-size: 16px;
          text-align: right;
        }

        .area_input {
          flex: 1.5;
          border: 1px solid #FF4444;
          border-radius: 4px;
          height: 38px;
          line-height: 38px;
          width: 100%;

          input {
            width: 100%;
            height: 38px;
            background-color: transparent;
            border: none;
            outline: none;
            text-indent: 10px;
            font-size: 14px;
            color: #CCCDCE;
          }

          input::placeholder {
            font-size: 14px;
            color: #CCCDCE;
          }
        }
      }

      .area_btns {
        width: 100%;
        padding-top: 40px;
        display: flex;
        justify-content: center;
        align-items: center;

        .area_btn {
          width: 136px;
          height: 40px;
          background: #FF4444;
          border-radius: 4px;
          font-weight: 500;
          font-size: 14px;
          line-height: 40px;
          text-align: center;
          color: #FFFFFF;
        }
      }

    }
  }

  .el-cascader {
    width: 100%;
    height: 38px;
  }
</style>
